<?php

namespace app\controllers;
use app\models\Menu;
use yii\data\ActiveDataProvider;
class MenuController extends \yii\web\Controller
{   
    public function behaviors()
    {   
        return [
            // 'verbs' => [
            //     'class' => VerbFilter::className(),
            //     'actions' => [
            //         'delete' => ['POST'],
            //     ],
            // ],
            'login' => [
                'class'   => \app\filters\LoginFilter::className()
            ],
        ];
    }

    public function actionIndex()
    {   
        $query = Menu::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => false
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider
        ]);
    }

    //新建顶级分类，子分类
    public function actionCreate()
    {   
        // $this->layout='@app/views/layouts/mian-pop.php';
        $pid = \Yii::$app->request->get('id', 0);
        
        $model = new Menu();

        if ($model->load(\Yii::$app->request->post()) && $model->save()) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            $ajaxdata['ok'] = 0;
        } else {
            //默认选中下拉框
            $model->pid = $pid;
            
            //所有分类数据
            $res = Menu::find()->asArray()->all();

            // $ajaxdata['ok'] = 0;
            return $this->renderAjax('_form', [
                'model' => $model,
                'data' => $model->getDropDownList($res),
                'action' => 'create',
            ]);
        }
        return $ajaxdata;
    }

    //删除分类
    public function actionDelete($id){
        Menu::deleteCategory($id);
        return $this->redirect(['index']);

    }

    //更新分类数据
    public function actionUpdate()
    {   
        // $this->layout='@app/views/layouts/mian-pop.php';

        $id = \Yii::$app->request->get('id', 0);
        $model = $this->findModel($id);

        if ($model->load(\Yii::$app->request->post()) && $model->save()) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            $ajaxdata['ok'] = 0;
        } else {
            //计算下拉框的数据
            $res = Menu::find()->asArray()->all();;

            // $ajaxdata['ok'] = 0;
            return $this->renderAjax('_form', [
                'model' => $model,
                'data' => $model->getDropDownList($res),
                'action' => 'update',
            ]);
        }
        return $ajaxdata;
    }

     /**
     * Finds the News model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return News the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Menu::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
